package cn.com.bluemoon.daps.model.core.file.validator;

import cn.com.bluemoon.daps.model.core.cache.ModelCacheHelper;
import cn.com.bluemoon.daps.system.entity.DapSystemTag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Optional;

/**
 * 表标签
 *
 * @author Jarod.Kong
 * @date 2021/3/15 16:07
 */
@Slf4j
@Deprecated
public class MyTableTagValidator implements ConstraintValidator<MyTableTag, CharSequence> {
    private static final String TAG_SPLIT_CHAR = "\\s+";

    @Override
    public void initialize(MyTableTag tableGroup) {

    }

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        String[] tags = value.toString().split(TAG_SPLIT_CHAR);
        for (String tag : tags) {
            Optional<DapSystemTag> dapSystemTag = ModelCacheHelper.findTagByNameInCache(tag);
            if (!dapSystemTag.isPresent()) {
                log.error("无法找到对应的系统标签");
                return false;
            }
        }
        log.debug("找到对应的系统标签[{}]", value);
        return true;
    }
}
